package com.tensquare.search.controller;

import com.tensquare.search.pojo.Article;
import com.tensquare.search.service.ArticleSearchService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Macross
 * @Type ArticleSearchController.java
 * @Desc
 * @date 2019/12/19 15:17
 */
@RestController
@RequestMapping("/article")
@Api(description = "文章搜索控制器")
public class ArticleSearchController {

    @Autowired
    private ArticleSearchService articleSearchService;

    /**
     * 添加文章信息到ES
     * @param article 文章信息
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "添加文章信息",notes = "添加文章至ES")
    public Result add(@RequestBody @ApiParam(value = "文章信息实体类") Article article){
        //添加文章
        articleSearchService.add(article);
        //返回请求结果
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /**
     * 通过关键字搜索文章,也就是对文章标题和文章内容进行模糊搜索
     * @param keyword 搜索关键字
     * @param page 分页页码
     * @param size 分页大小
     * @return
     */
    @GetMapping("/search/{keyword}/{page}/{size}")
    @ApiOperation(value = "通过关键字搜索文章",notes = "对文章标题和文章内容进行模糊搜索")
    public Result<PageResult<Article>> findByTitleOrContentLike(@PathVariable @ApiParam(value = "搜索关键字") String keyword,
                                                                @PathVariable @ApiParam(value = "分页页码") Integer page,
                                                                @PathVariable @ApiParam(value = "分页大小") Integer size){
        //获取分页后的搜索结果
        PageResult<Article> result = articleSearchService.findByTitleOrContentLike(keyword,page,size);
        //将结果进行封装并返回
        return new Result<>(true,StatusCode.OK,"搜索成功",result);
    }
}
